package com.wtz.temptest;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import static java.time.format.DateTimeFormatter.ISO_INSTANT;

/**
 * @author tiezhu
 * Company dtstack
 * Date 2020/8/11 星期二
 */
public class TempTest {
    public static LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(1);

    private static final Pattern DATETIME = Pattern.compile("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))([0-1]?[0-9]|2[0-3])([0-5][0-9])([0-5][0-9])$\n");
    private static final int MILLIS_PER_SECOND = 1000;

    public static void main(String[] args) throws ParseException {
        String timeStr = "2021-02-04 17:56:06.002";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = format.parse(timeStr);
        System.out.println(parse);
        if (DATETIME.matcher(timeStr).find()) {
            Instant instant = Instant.from(ISO_INSTANT.parse(timeStr));
            System.out.println(new Timestamp(instant.getEpochSecond() * MILLIS_PER_SECOND));
        }
//        String str = "1946-06-24";
//        Date utilDate = transformStringToDate(str);
//        java.sql.Date sqlDate = java.sql.Date.valueOf(str);
//        System.out.println(utilDate);
//        Instant instant = utilDate.toInstant();
//        System.out.println(instant);
//        System.out.println("-------------");
//        System.out.println(sqlDate);
//        testBlockingQueue();
    }

    private static Date transformStringToDate(String s) throws ParseException {
        Date date;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        date = dateFormat.parse(s);
        return date;
    }

    private static void testBlockingQueue() {
        new Producer().start();

        new Consumer().start();
    }
    static class Producer extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    queue.put("test");
                    System.out.println("queue size: " + queue.size());
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Consumer extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String poll = queue.poll();
                System.out.println("get data " + poll);
            }
        }
    }
}
